package com.haiheng.common.dialog

import android.app.Dialog
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.*
import com.haiheng.library.common.databinding.StandardDialogBinding
import com.haiheng.library_base.dialog.BaseDialog
import com.haiheng.library_base.utils.MetricsUtils
import com.haiheng.library_base.utils.ScreenRecordHelper

/**
 *
 * @Des:标准对话框
 * @Author: hwz
 * @Date:2022/4/18 0018
 * @Version: 1.0
 */
class StandardDialog : BaseDialog() {
    companion object{

        @JvmStatic
        fun newInstance(): StandardDialog {
            return StandardDialog()
        }
    }

    private lateinit var binding : StandardDialogBinding
    private var listener: OnDialogClickListener? = null
    private var title:String? = null
    private var content:String? = null
    private var data:Any? = null
    private var leftText:String? = null
    private var rightText:String? = null
    private var isHindenCancelBtn:Boolean = false
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        dialog?.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        binding = StandardDialogBinding.inflate(inflater,container,false)
        return binding.root
    }




    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        dialog?.window?.apply { setDimAmount(0.5f) }
        resizeDialogFragment()
        title?.apply {
            binding.title.text = this
            binding.content.visibility = View.VISIBLE
        }
        content?.apply {
            binding.content.text = this
            binding.content.visibility = View.VISIBLE
        }
        leftText?.apply {
            binding.cancel.text = this
        }
        rightText?.apply {
            binding.confirm.text = this
        }
        binding.confirm.setOnClickListener{
            dismiss()
            listener?.apply {
                onConfirm(this@StandardDialog)
            }
        }
        binding.cancel.setOnClickListener{
            dismiss()
            listener?.apply {
                onCancel(this@StandardDialog)
            }
        }
        binding.cancel.visibility = if (isHindenCancelBtn) View.GONE else View.VISIBLE
        binding.vLine.visibility = if (isHindenCancelBtn) View.GONE else View.VISIBLE
    }
    private fun resizeDialogFragment() {
        val dialog: Dialog? = dialog
        if (null != dialog) {
            val window= dialog?.getWindow()
            val lp: WindowManager.LayoutParams = getDialog()!!.window!!.attributes
//            lp.height = MetricsUtils.getScreenHeight(requireContext())-MetricsUtils.dp2px(70f).toInt()
            lp.width = MetricsUtils.getScreenWidth(requireContext())-MetricsUtils.dp2px(140f).toInt()
            if (window != null) {
                window.setLayout(lp.width, lp.height)
            }
        }
    }

    fun setIsHindenCancel(isHinden:Boolean):StandardDialog{
        this.isHindenCancelBtn = isHinden
        return this
    }
    fun setTitle(text:String):StandardDialog{
        this.title = text
        if (::binding.isInitialized){
            binding?.title?.setText(text)
        }
        return this
    }

    fun setContent(text:String):StandardDialog{
        this.content = text
        if (::binding.isInitialized){
            binding?.content?.setText(text)
        }
        return this
    }

    fun setData(textDate:Any):StandardDialog{
        this.data = textDate
        return this
    }
    fun getData():Any?{
        return data
    }
    fun setLeftText(text: String):StandardDialog{
        this.leftText = text
        if (::binding.isInitialized){
            binding?.cancel?.setText(text)
        }
        return this
    }
    fun  setRightText(text: String):StandardDialog{
        this.rightText = text
        if (::binding.isInitialized){
            binding?.confirm?.setText(text)
        }
        return this
    }

    fun setOnDialogListener(listener: OnDialogClickListener):StandardDialog{
        this.listener = listener
        return this
    }
    interface OnDialogClickListener {
        fun onConfirm(dialog: StandardDialog)
        fun onCancel(dialog: StandardDialog)
    }

}